package com.dsbj.mp.icardservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.Reservation;
import com.dsbj.mp.icardservice.entity.ReservationPrimary;
import com.dsbj.mp.icardservice.model.MpUser;
import com.dsbj.mp.icardservice.model.ReservationNoModel;
import com.dsbj.mp.icardservice.service.ReservationPrimaryService;
import com.dsbj.mp.icardservice.service.ReservationService;
import com.dsbj.mp.icardservice.utils.jwt.JWTInfo;
import com.dsbj.mp.icardservice.utils.strprocess.StrUtil;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.AllowUser;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.AllowUserResponseMessage;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.JWSService;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.WxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reservation")
public class ReservationController {

	@Autowired
	private ReservationService reservationService;

	@Resource
	private ReservationPrimaryService reservationPrimaryService;

	@Resource
	private JWSService jwsService;


	/**
	 * 根据creatorEmployeeNo获取其创建的访客单列表，并分页。
	 *
	 * @param
	 * @return
	 * @author cjw 2019-10-09，changed on 2019-12-02
	 */
	@RequestMapping("/listReservationPrimaryWithPage")
	public ResponseMessage listReservationPrimaryWithPage(Integer pagesize, Integer pagenum, HttpServletRequest request) {

		/**
		 * 解析传来的参数
		 */
		//JWTInfo jwtInfo=(JWTInfo)request.getAttribute("jwtInfo");
		MpUser wxUser=(MpUser)request.getAttribute("wxUser");
		String creatorEmployeeNo=wxUser.getEmployeeNo();

		//若pagesize,pagenum为null，赋初始值。
		if(null==pagesize){
			pagesize=10;
		}
		if(null==pagenum){
			pagenum=1;
		}
		if (null == pagesize || pagesize < 1 || null == pagenum || pagenum < 0) {
			return ResponseMessage.error("传入的参数有误!");
		}

		/**
		 * 获取其创建的访客单列表，并分页
		 */
		Page<ReservationPrimary> page = reservationPrimaryService.getReservationPrimaryByPage(creatorEmployeeNo, pagesize, pagenum);
		//输出reservationPrimaryPage
		//Page page1=OutputReservationPrimary.outputReservationPrimaryPage(page);
		return ResponseMessage.ok(page);
	}

	/**
	 * 例子。
	 * @deprecated 待删
	 * @param id
	 * @return
	 */
	@Deprecated
	@RequestMapping("/getReservationById")
	public List<Reservation> getReservationById(String id) {
		return reservationService.getReservationById(id);
	}

	/**
	 * 根据id获取访客预约单详情。
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	@RequestMapping(value = "/getReservationPrimaryById")
	public ResponseMessage getReservationPrimaryById(String id){

		/**
		 * 解析传来的参数
		 */
		if (StrUtil.isEmpty(id)) {
			return ResponseMessage.error("传入的参数有误!");
		}

		/**
		 * 根据id获取访客预约单详情。
		 */
		ReservationPrimary reservationPrimary=reservationPrimaryService.getReservationPrimaryById(id);
		return ResponseMessage.ok(reservationPrimary);
	}

	/**
	 * 根据reservno绘制二维码，并获取其对应的base64格式的字符串。
	 * @param
	 * @return
	 * @author cjw 2019-12-16
	 */
	@RequestMapping(value = "/getReservnoQrcode")
	public ResponseMessage getReservnoQrcode(String reservno){
		if(StrUtil.isEmpty(reservno)) {
			return ResponseMessage.error("传入的参数有误!");
		}
		String reservnoQrcodeStr=reservationService.generateQrBase64(reservno);
		//返回结构体
		Map<String,Object> mapReturn=new LinkedHashMap<>();
		mapReturn.put("reservnoQrcodeStr",reservnoQrcodeStr);
		return ResponseMessage.ok(mapReturn);
	}

	@RequestMapping("/getNewReservationNo")
	public ResponseMessage<ReservationNoModel> getNewReservationNo() {
		return ResponseMessage.ok(reservationService.getNewReservationno());
	}

	/**
	 * 存入reservationPrimary。
	 * @deprecated 待删
	 * @param map
	 * @return
	 */
	@RequestMapping("/save_0")
	public ResponseMessage<String> save_0(@RequestBody Map<String, JSONObject> map) {
		AllowUser user = map.get("user").toJavaObject(AllowUser.class);
		ReservationPrimary reservationPrimary = map.get("reservationPrimary").toJavaObject(ReservationPrimary.class);

		if (null == user || null == reservationPrimary) {
			return ResponseMessage.error("传入参数有误");
		}
		return reservationService.save(user, reservationPrimary);
	}

	/**
	 * 存入reservationPrimary信息。
	 * @param
	 * @return
	 * @author cjw 2019-12-09
	 */
	@RequestMapping(value = "/saveReservationPrimary", method = RequestMethod.POST)
	public ResponseMessage<String> save(@RequestBody Map<String, Object> map, HttpServletRequest request) {
		//StrUtil.prettyPrintWithTime("map",map);

		/**
		 * 根据wxUser.employeeNo获取allowUser
		 */
		MpUser wxUser=(MpUser) request.getAttribute("wxUser");
		AllowUserResponseMessage allowUserResponseMessage=jwsService.getUserByEmployeeno(wxUser.getEmployeeNo());
		if(allowUserResponseMessage==null||allowUserResponseMessage.getAllowUser()==null){
			return ResponseMessage.error("未找到employeeno对应的allowUser信息!");
		}
		AllowUser allowUser=allowUserResponseMessage.getAllowUser();

		/**
		 * 根据传入的参数创建reservationPrimary。
		 * 此处未检验传入的参数是否完整。
		 */
		String jsonstr=StrUtil.tojsonstr(map);
		ReservationPrimary reservationPrimary=JSON.parseObject(jsonstr,new TypeReference<ReservationPrimary>(){}, Feature.OrderedField);
		return reservationService.save(allowUser,reservationPrimary);
	}




	/**
	 *
	 * @param
	 * @return
	 * @author cjw 2019-12-09
	 */
	public static void main(String[] args){

	}

}
